package com.soft.servlet;


import com.soft.dao.ProfileDAO;
import com.soft.entity.Profile;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet({"/ProfileServlet"})
public class ProfileServlet
        extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String uri;

    public void destroy() {
        super.destroy();
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        String method = request.getParameter("method");


        if ("list".equals(method)) {

            list(request, response);
        } else if ("add".equals(method)) {

            add(request, response);
        } else if ("delete".equals(method)) {

            delete(request, response);
        } else if ("Jump".equals(method)) {

            Jump(request, response);
        } else if ("update".equals(method)) {

            update(request, response);
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            ProfileDAO dao = new ProfileDAO();
            Profile result = dao.getAll();

            request.setAttribute("result", result);

            this.uri = "/sys/profile/profile.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String Content = request.getParameter("content");
            Profile profile = new Profile();
            profile.setContent(Content);

            ProfileDAO dao = new ProfileDAO();
            dao.add(profile);

            this.uri = "/ProfileServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int Id = Integer.parseInt(request.getParameter("id"));
            Profile profile = new Profile();
            profile.setId(Id);

            ProfileDAO dao = new ProfileDAO();
            dao.delete(profile);

            this.uri = "/ProfileServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void Jump(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int Id = Integer.parseInt(request.getParameter("id"));
            Profile profile = new Profile();
            profile.setId(Id);

            ProfileDAO dao = new ProfileDAO();
            Profile result = dao.getone(profile);

            request.setAttribute("result", result);

            this.uri = "/sys/profile/update.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }


    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int Id = Integer.parseInt(request.getParameter("id"));
            String Content = request.getParameter("content");
            Profile profile = new Profile();
            profile.setId(Id);
            profile.setContent(Content);

            ProfileDAO dao = new ProfileDAO();
            dao.update(profile);

            this.uri = "/ProfileServlet?method=list";
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher(this.uri).forward((ServletRequest) request, (ServletResponse) response);
    }

    public void init() throws ServletException {
    }
}
